<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="用车标题" prop="title">
				<el-input v-model="form.title" placeholder="用车标题" maxlength="80" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="车辆类型" prop="carType">
						<sc-select v-model="form.carType" :params="{code:'vehicleType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="用车人" prop="usedUser">
						<select-user-dialog v-model:accountId="form.usedUser" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="用车事由" prop="cause">
				<el-input v-model="form.cause" placeholder="用车事由" maxlength="300" type="textarea" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="出发地点" prop="sourceAddress">
						<el-input v-model="form.sourceAddress" placeholder="出发地点" maxlength="100" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="目的地" prop="destination">
						<el-input v-model="form.destination" placeholder="目的地" maxlength="100" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="大约里程" prop="mileage">
						<el-input-number v-model="form.mileage" placeholder="大约里程" clearable></el-input-number>
						/公里
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否需要油卡" prop="card">
						<el-radio-group v-model="form.card">
							<el-radio-button label="0">不需要</el-radio-button>
							<el-radio-button label="1">需要</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" :disabled-date="publishDateAfter" placeholder="开始时间" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" :disabled-date="publishDateAfter" placeholder="结束时间" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="是否需要司机" prop="driver">
						<el-radio-group v-model="form.driver">
							<el-radio-button label="0">不需要</el-radio-button>
							<el-radio-button label="1">需要</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="调度人员" prop="optUser">
						<sc-select v-model="form.optUser" :myProps="{label:'userName',value:'accountId'}" :apiObj="$API.office.vehicle.getVehicleOperatorUserList" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="随车人员" prop="passenger">
						<el-input-number v-model="form.passenger" placeholder="随车人员" clearable></el-input-number>
						/人
					</el-form-item>
				</el-col>
				<el-col :lg="12">
				</el-col>
			</el-row>
			<el-form-item label="备注说明" prop="remark">
				<el-input v-model="form.remark" placeholder="备注说明" maxlength="300" type="textarea" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";

export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '车辆使用申请',
				edit: '车辆使用申请',
			},
			fileList: [],
			form: {
				applyId: '',
				carType: '',
				title: '',
				usedUser: '',
				cause: '',
				beginTime: '',
				endTime: '',
				sourceAddress: '',
				destination: '',
				mileage: 0,
				card: '0',
				driver: '0',
				optUser: '',
				passenger: '',
				remark: ''
			},
			rules: {
				title: [
					{required: true, message: '用车标题不能为空', trigger: 'blur'}
				],
				carType: [
					{required: true, message: '车辆类型不能为空', trigger: 'blur'}
				],
				cause: [
					{required: true, message: '用车事由不能为空', trigger: 'blur'}
				],
				usedUser: [
					{required: true, message: '用车人不能为空', trigger: 'blur'}
				],
				beginTime: [
					{required: true, message: '开始时间不能为空', trigger: 'blur'}
				],
				endTime: [
					{required: true, message: '结束时间不能为空', trigger: 'blur'}
				],
				sourceAddress: [
					{required: true, message: '出发地点不能为空', trigger: 'blur'}
				],
				optUser:[
					{required: true, message: '请选择调度人员', trigger: 'blur'}
				],
				destination: [
					{required: true, message: '目的地不能为空', trigger: 'blur'}
				],
				passenger: [
					{required: true, message: '随车人员不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		publishDateAfter(time) {
			return time.getTime() < (Date.now() - 24 * 60 * 60 * 1000)
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.vehicle.insertVehicleApply.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.vehicle.updateVehicleApply.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.applyId = data.applyId
			this.form.carType = data.carType
			this.form.title = data.title
			this.form.usedUser = data.usedUser
			this.form.cause = data.cause
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.sourceAddress = data.sourceAddress
			this.form.destination = data.destination
			this.form.mileage = data.mileage
			this.form.card = data.card
			this.form.driver = data.driver
			this.form.optUser = data.optUser
			this.form.passenger = data.passenger
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
